package cn.mw.cmdb.controller;

import cn.mw.cmdb.entity.AssetsInterfaceDTO;
import cn.mw.cmdb.param.AssetsScanFailParam;
import cn.mw.cmdb.param.AssetsScanParam;
import cn.mw.cmdb.param.MwAssetsInterfaceParam;
import cn.mw.cmdb.param.SearchAssetsInterfaceParam;
import cn.mw.cmdb.service.AssetsManageService;
import cn.mw.cmdb.template.param.ZabbixHandlerParam;
import cn.mw.cmdb.template.service.MwZabbixHandlerService;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.microMonitorCommon.api.cmdb.AssetCatologParam;
import cn.mw.microMonitorCommon.api.cmdb.AssetsSearchParam;
import cn.mw.microMonitorCommon.api.cmdb.AssetsService;
import cn.mw.microMonitorCommon.api.cmdb.ItemNameMapperParam;
import cn.mw.microMonitorCommon.api.common.BaseApiService;
import cn.mw.microMonitorCommon.api.common.ResponseBase;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/assetsManage")
@Controller
@Tag(name = "资产管理")
@Slf4j
public class AssetsManageController extends BaseApiService {

    private AssetsManageService assetsManageService;
    private MwZabbixHandlerService mwZabbixHandlerService;
    public static final String SHOW_FLAG_KEY = "show";
    public static final String ALERT_FLAG_KEY = "alert";
    public static final String ITEM_FLAG_KEY = "item";




    @Autowired
    private AssetsService assetsService;

    public AssetsManageController(AssetsManageService assetsManageService, MwZabbixHandlerService mwZabbixHandlerService) {
        this.assetsManageService = assetsManageService;
        this.mwZabbixHandlerService = mwZabbixHandlerService;
    }

    @PostMapping("/listCatolog")
    @ResponseBody
    @Operation(summary = "查看目录")
    public ResponseBase listCatolog(@RequestBody @Validated AssetCatologParam param) throws Exception {
        Object ret = assetsService.listAssetCatolog(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/getBaseAssetsTree")
    @ResponseBody
    @Operation(summary = "获取基础设施树结构")
    public ResponseBase getBaseAssetsTree(@RequestBody AssetCatologParam param) throws Exception {
        Object ret = assetsService.getBaseAssetsTree(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/searchAssets")
    @ResponseBody
    @Operation(summary = "搜索资产")
    public ResponseBase searchAssets(@RequestBody AssetsSearchParam param) throws Exception {
        Object ret = assetsService.searchAssets(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/updateInstanceMonitorStatus")
    @ResponseBody
    @Operation(summary = "更新资产告警状态")
    public ResponseBase updateInstanceMonitorStatus(@RequestParam String instanceId, @RequestParam String alertLevel) throws Exception {
        assetsService.updateInstanceMonitorStatus(instanceId, alertLevel);
        return setResultSuccess();
    }

    @PostMapping("/getItemNameMappingInfo")
    @ResponseBody
    @Operation(summary = "获取item指标中文名称")
    public ResponseBase getItemNameMappingInfo(@RequestBody ItemNameMapperParam param) throws Exception {
        Object ret = assetsService.getItemNameMappingInfo(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/failRescan")
    @ResponseBody
    @Operation(summary = "失败任务重新扫描")
    public ResponseBase rescan(@RequestBody AssetsScanFailParam param) throws Exception {
        Object ret = assetsManageService.failAssetsScan(param);
        return setResultSuccess(ret);
    }

    @GetMapping("/refreshIfConfig")
    @ResponseBody
    @Operation(summary = "更新网络接口信息")
    public ResponseBase refreshIfConfig() throws Exception {
        assetsManageService.refreshAssetsIfInfo();
        return setResultSuccess();
    }

    @GetMapping("/refreshIfConfigByInstance")
    @ResponseBody
    @Operation(summary = "更新单实例网络接口信息")
    public ResponseBase refreshIfConfigByInstance(String instanceId) throws Exception {
        assetsManageService.refreshAssetsIfInfoByInstanceId(instanceId);
        return setResultSuccess();
    }

    @PostMapping("/getAssetsInterfaceInfo")
    @ResponseBody
    @Operation(summary = "获取实例网络接口信息")
    public ResponseBase getAssetsInterfaceInfo(@RequestBody SearchAssetsInterfaceParam param) throws Exception {
        Object ret = assetsManageService.getAssetsInterface(param.getInstanceIds());
        return setResultSuccess(ret);
    }

    @PostMapping("/editInterfaceSettingFlag")
    @ResponseBody
    @Operation(summary = "更新网络接口设置状态")
    public ResponseBase editInterfaceSettingFlag(@RequestBody MwAssetsInterfaceParam param) throws Exception {
        //设置显示状态
        assetsManageService.editInterfaceFlag(param);
        ZabbixHandlerParam zabbixHandlerParam = new ZabbixHandlerParam();
        zabbixHandlerParam.setInterfaceNames(param.getInterfaceNames());
        zabbixHandlerParam.setServerId(param.getMonitorServerId());
        zabbixHandlerParam.setHostId(param.getHostId());

        //更新自动发现规则过滤信息(状态为显示的才生成自动发现规则)
        if (SHOW_FLAG_KEY.equals(param.getType())) {
            zabbixHandlerParam.setDisplayInterfaceList(param.getDisplayInterfaceNames());
            zabbixHandlerParam.setStatus(param.getShowFlag());
            //关闭自动发现规则，默认同时关闭告警触发器
            mwZabbixHandlerService.updateDiscoveryRuleFilterInfo(zabbixHandlerParam);
            //关闭告警触发器
            mwZabbixHandlerService.interfaceTriggerDeactivate(zabbixHandlerParam);
            mwZabbixHandlerService.updateMonitorItemStatus(zabbixHandlerParam);
        }
        //告警状态修改
        if (ALERT_FLAG_KEY.equals(param.getType())) {
            zabbixHandlerParam.setStatus(param.getAlertFlag());
            mwZabbixHandlerService.interfaceTriggerDeactivate(zabbixHandlerParam);
        }
        //item监控状态修改
        if (ITEM_FLAG_KEY.equals(param.getType())) {
            zabbixHandlerParam.setStatus(param.getItemFlag());
            mwZabbixHandlerService.updateMonitorItemStatus(zabbixHandlerParam);
        }
        return setResultSuccess();
    }

    @PostMapping("/editInterfaceDescInfo")
    @ResponseBody
    @Operation(summary = "更新网络接口描述信息")
    public ResponseBase editInterfaceDesc(@RequestBody AssetsInterfaceDTO param) throws Exception {
        //设置显示状态
        assetsManageService.editInterfaceDesc(param);
        return setResultSuccess();
    }

    @PostMapping("/deleteInterfaceInfo")
    @ResponseBody
    @Operation(summary = "删除网络接口描述信息")
    public ResponseBase deleteInterfaceInfo(@RequestBody MwAssetsInterfaceParam param) throws Exception {
        //接口显示状态设置为false
        assetsManageService.deleteInterfaceInfo(param);
        ZabbixHandlerParam zabbixHandlerParam = new ZabbixHandlerParam();
        zabbixHandlerParam.setInterfaceNames(param.getInterfaceNames());
        zabbixHandlerParam.setServerId(param.getMonitorServerId());
        zabbixHandlerParam.setHostId(param.getHostId());
        //更新自动发现规则过滤信息(状态为显示的才生成自动发现规则)
        zabbixHandlerParam.setStatus(false);
        //关闭自动发现规则，默认同时关闭告警触发器
        mwZabbixHandlerService.updateDiscoveryRuleFilterInfo(zabbixHandlerParam);
        //关闭告警触发器
        mwZabbixHandlerService.interfaceTriggerDeactivate(zabbixHandlerParam);
        return setResultSuccess();
    }

    @PostMapping("/getAssetAllInterfaceByPage")
    @ResponseBody
    @Operation(summary = "分页查询实例所有接口列表")
    public ResponseBase getAssetAllInterfaceByPage(@RequestBody SearchParam param) throws Exception {
        Object ret = assetsManageService.getAssetAllInterfaceByPage(param);
        return setResultSuccess(ret);
    }

    @PostMapping("/getAssetShowInterfaceByPage")
    @ResponseBody
    @Operation(summary = "分页查询显示接口列表")
    public ResponseBase getAssetShowInterfaceByPage(@RequestBody SearchParam param) throws Exception {
        Object ret = assetsManageService.getAssetShowInterfaceByPage(param);
        return setResultSuccess(ret);
    }

    @PostMapping("getAssetInterfaceStatusCount")
    @ResponseBody
    @ApiOperation(value = "接口状态汇总统计")
    public ResponseBase<Map<String, Integer>> getAssetInterfaceStatusCount(@RequestBody SearchParam param) throws Exception {
        return setResultSuccess(assetsManageService.getAssetInterfaceStatusCount(param));
    }

    @PostMapping("initInterfaceSettingFlag")
    @ResponseBody
    @ApiOperation(value = "初始化接口设置")
    public ResponseBase initInterfaceSettingFlag() throws Exception {
        assetsManageService.initInterfaceSettingFlag();
        return setResultSuccess();
    }
}
